pwc.CalendarNoteCache = function() {
    this._cache = {};
};

pwc.CalendarNoteCache.prototype = {
    updateNote: function(calendarNote){
        var year = calendarNote.getDate().getFullYear();
        if(!this._cache[year]){
            this._cache[year] = [];
        }
        if(calendarNote.getId() == ""){
            this._deleteNote(calendarNote.getDate());
            return;
        }

        var existingNote = this._cache[year].findFirst(function(note){
            return note.getId() == calendarNote.getId();
        });
        if(existingNote){
            existingNote.setContent(calendarNote.getContent());
        }else{
            this._addNote(calendarNote);
        }
    },

    _deleteNote:function(date){
        var year = date.getFullYear();
        this._cache[year] = this._cache[year].where(function(note){
            return !note.getDate().equals(date);
        });
    },

    _addNote: function(calendarNote){
        var year = calendarNote.getDate().getFullYear();
        if(this._cache[year]){
            this._cache[year].push(calendarNote);
        }else{
            this._cache[year] = [calendarNote];
        }
    },

    setNotesByYear: function(year, notes){
        this._cache[year] = notes;
    },

    getNoteByDate: function(date){
        var year = date.getFullYear();
        if(!this._cache[year]){
            return null;
        }

        return this._cache[year].findFirst(function(note){
            return note.getDate().sameDay(date);
        });
    },

    getNotesByYear: function(year){
        return this._cache[year];
    }
};